import store from '@/store/index'
import Vue from 'vue';
import data from "./test.js"

import VueI18n from 'vue-i18n'

let i18n = null;
setTimeout(() => {
	i18n = new VueI18n({
		locale: store.state.lang ? store.state.lang.toString() : 'zh', // 语言标识
		messages: {
			'zh': require('@/common/lang/zh'), // 中文简体语言包
			'en': require('@/common/lang/en'), // 中文简体语言包
		},
		silentTranslationWarn: true,
	})
}, 200)



const globalRequest = (api, method, options) => {
	let loadingTimer = null;
	const option = {
		loading: true, //是否显示loading
		delayed: true, //是否延时显示loading
		loading_text: 'loading', //是否延时显示loading
		params: {}
	}
	if (options) {
		if (options.loading !== undefined) option.loading = options.loading
		if (options.delayed !== undefined) option.delayed = options.delayed
		if (options.loading_text !== undefined) option.loading_text = options.loading_text
		if (options.params !== undefined) option.params = options.params
	}


	let request_url = Vue.prototype.$config.baseURL + api;
	if (/^http/.test(api)) {
		request_url = api
	} else if (/^test/.test(api)) {
		return new Promise((resolve, reject) => {
			setTimeout(() => {
				if (options && options.params && options.params.page && options.params.page >= 3) {
					data.data.next = 0
				} else {
					data.data.next = 1
				}
				resolve(data.data)
			}, 300)
		})
	}
	const headers = {
		"Content-Type": 'application/x-www-form-urlencoded',
		"token": localStorage.getItem("token") || '', //token
		"lang": uni.getStorageSync('lang') || '', //lang
	}

	if (option.loading && !option.delayed) {
		// 立即显示loading
		store.commit("loadingShow", option.loading_text)
	} else if (option.loading && option.delayed) {
		// 延时显示loading
		loadingTimer = setTimeout(() => {
			store.commit("loadingShow", option.loading_text)
		}, 1000)
	}

	return new Promise(function(resolve, reject) {
		uni.request({
			url: request_url,
			method: method,
			data: option.params,
			dataType: 'json',
			withCredenttials: true,
			header: headers,
			success: function(res) {

				clearTimeout(loadingTimer)
				store.commit("loadingHide") //隐藏loading
				if (res.statusCode == 200) { //浏览器200

					if (res.data.code === 0) {
						resolve(res.data)
					} else {
						reject(res.data)
					}

					if (res.data.code == -1) {
						store.commit("loginout")
						reject({
							msg: i18n ? i18n.t('请先登录') : "please log in first"
						})
					}

				} else {
					reject({
						msg: i18n ? i18n.t('请检查网络') : "error"
					}) // 请检查网络 // 浏览器处理不是200
				}
			},
			fail: (err) => {
				clearInterval(loadingTimer)
				store.commit("loadingHide") //隐藏loading
				if (store.state.network) {

					uni.showModal({
						content: i18n ? i18n.t('请检查网络') : "error", //请检查网络
						confirmText: i18n ? i18n.t('确认') : "Confirm",
						showCancel: false,
					});
					store.commit('setNetwork', false)

					setTimeout(() => {
						store.commit('setNetwork', true)
					}, 5000)
				}
				reject({
					msg: i18n ? i18n.t('请检查网络') : "error"
				}) //请检查网络
			}
		})
	})
}



// import request from '@/common/request.js'
const api = {
	post: (api_path, options) => globalRequest(api_path, 'POST', options),
	get: (api_path, options) => globalRequest(api_path, 'GET', options)
}

export default api